/**
 * Copyright (C) <2023> <Boundivore> <boundivore@foxmail.com>
 * <p>
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the Apache License, Version 2.0
 * as published by the Apache Software Foundation.
 * <p>
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * Apache License, Version 2.0 for more details.
 * <p>
 * You should have received a copy of the Apache License, Version 2.0
 * along with this program; if not, you can obtain a copy at
 * http://www.apache.org/licenses/LICENSE-2.0.
 */
import { useSearchParams } from 'react-router-dom';
import { t } from 'i18next';
import APIConfig from '@/api/config';
import RequestHttp from '@/api';
import { useConfigState } from './useConfigState';
import { useConfigProcessor } from './useGroupManager';
import { MessageInstance } from 'antd/lib/message/interface';

export const useFileContent = (messageApi: MessageInstance, callback?: () => void) => {
	const [searchParams] = useSearchParams();
	const id = searchParams.get('id');
	const serviceName = searchParams.get('name') || '';
	const { configGroupInfo, setIsConfigModified } = useConfigState();
	const { processEditorData } = useConfigProcessor();

	const saveChange = async (customValue?: boolean) => {
		const api = APIConfig.saveByGroup;
		!customValue && processEditorData();

		const params = {
			ClusterId: id,
			ConfigGroupList: configGroupInfo,
			ServiceName: serviceName
		};
		const data = await RequestHttp.post(api, params);
		const { Code } = data;
		if (Code === '00000') {
			messageApi.success(t('messageSuccess'));
			setIsConfigModified(false);
			callback && callback();
		}
	};
	return {
		saveChange
	};
};
